package com.controller;

import com.pojo.ElectronicFile;
import com.service.ElectronicFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author Mr.Shang
 * @version 1.0
 * @date 2021/9/15 12:05
 * @Description 电子文档控制层
 */

@Controller
@RequestMapping("file")
public class ElectronicFileController {
    @Autowired
    private ElectronicFileService fileService;

//    @RequestMapping("findAll")
//    public void testFindAll(){
//        List<ElectronicFile> files = fileService.findAll();
//        System.out.println(files);
//    }

//    @RequestMapping("findAll")
//    public ModelAndView modelAndView(){
//        List<ElectronicFile> files = fileService.findAll();
//
//        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("files", files);
//        modelAndView.setViewName("/listfile.jsp");
//
//        return modelAndView;
//    }

    @ResponseBody
    @RequestMapping("findAll")
    public Map<String, Object> findAll() {
        List<ElectronicFile> files = fileService.findAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg", "success");
        map.put("code", 0);
        map.put("count", files.size());
        map.put("data", files);

        return map;
    }

    @RequestMapping("add")
    public void add(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String filename = request.getParameter("filename");
        String digest = request.getParameter("digest");
        String username = request.getParameter("username");
        String date = request.getParameter("date");

        ElectronicFile file = new ElectronicFile();
        file.setFilename(filename);
        file.setDigest(digest);
        file.setUsername(username);
        file.setDate(new Date(date));

        fileService.add(file);
        response.sendRedirect("/filelist.html");
    }

    @RequestMapping("delete")
    public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        fileService.delete(Integer.parseInt(id));
        response.sendRedirect("/filelist.html");
    }

    @RequestMapping("findById")
    public void findById(Integer id,HttpServletResponse response,HttpServletRequest request) throws ServletException, IOException {
        ElectronicFile file = fileService.findById(id);
        request.setAttribute("data",file);
        request.getRequestDispatcher("/WEB-INF/jsp/update.jsp").forward(request,response);
    }

    @RequestMapping("update")
    public void update(HttpServletResponse response,HttpServletRequest request) throws IOException {
        String id = request.getParameter("id");
        String filename = request.getParameter("filename");
        String digest = request.getParameter("digest");
        String username = request.getParameter("username");
        String date = request.getParameter("date");

        ElectronicFile file = new ElectronicFile();
        file.setId(Integer.parseInt(id));
        file.setFilename(filename);
        file.setDigest(digest);
        file.setUsername(username);
        file.setDate(new Date(date));

        fileService.update(file);
        response.sendRedirect("/filelist.html");
    }
}
